<template>
	<div class="export-dialog-view" v-loading="pageLoading">
		<div class="export-dialog">
			<div class="left-lay">
				<div class="split-title">{{ title }}</div>
				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.device") }}
					</div>
					<div class="line-value">{{ $store.state.plantName }}</div>
				</div>

				<div class="left-line">
					<div class="line-title">
						{{ $t("message.bhView.leftComponents.object") }}
					</div>
					<div class="line-value">
						{{
							$store.state.macType == 1
								? $t("message.bhView.leftComponents.machine")
								: $t("message.bhView.leftComponents.piping")
						}}
					</div>
				</div>

				<div class="left-line">
					<div class="line-title">
						{{ $t("message.reportFrom.exportDialog.title_c") }}
					</div>
					<div class="line-value">{{ fileTypeStr }}</div>
				</div>



				<div class="left-draw" id="left-draw"></div>
			</div>

			<div class="tip-lay"></div>

			<div class="right-lay">
				<div class="split-title text-right">
					<i class="el-icon-close" @click="CloseExprotDiloag"></i>
				</div>
				<div class="split-title">
					{{ $t("message.reportFrom.exportDialog.title_l") }}
				</div>
				<div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_C.lab1") }}
					</div>
					<div class="line-content">
						<el-date-picker v-model="StartDate" type="month" :format="format" :value-format="format"
							placeholder=" " class="ipt picker">
						</el-date-picker>
						<span>~</span>
						<el-date-picker v-model="EndDate" type="month" :format="format" :value-format="format"
							placeholder=" " class="ipt picker orange">
						</el-date-picker>
					</div>
				</div>

				<div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_C.lab2") }}
					</div>
					<div class="line-content">
						<el-select v-model="exeTim" placeholder="请选择" class="ipt picker">
							<el-option v-for="(item, index) in exeTimList" :key="index" :label="item" :value="index">
							</el-option>
						</el-select>
					</div>
				</div>


				<div class="right-line">
					<div class="line-title">
						{{ $t("message.ReportFormTable.D_C.lab3") }}
					</div>
					<div class="line-content" style="padding-left:12px;">
						<el-radio-group v-model="PrintType">
							<el-radio :label="1">{{ $t("message.ReportFormTable.D_C.lab4") }}</el-radio>
							<el-radio :label="2">{{ $t("message.ReportFormTable.D_C.lab5") }}</el-radio>
						</el-radio-group>
					</div>
				</div>





			</div>
			<footer class="footer-view">
				<div class="">
					<div class="line-title" style="width: 100%">
						{{ $t("message.reportFrom.exportDialog.tip") }}
					</div>
				</div>
				<div>
					<el-button @click="CloseExprotDiloag">Cancel</el-button>
					<el-button type="primary" @click="onExportClick">OK</el-button>
				</div>
			</footer>
		</div>
	</div>
</template>

<script>
import {
	ExResOfferDrawReportVerify,
	ExResOfferDrawReport,
	CollectFiles,
	getOfferDetialCombox
} from "@/api/api";
import * as svg_opt from "@/utils/svg_opt.js";
import { getLocalStorage } from "@/utils/storage";

export default {
	props: ["title", "fileTypeStr", "selectMacData", "fileRadio"],
	name: "D_C_Export",
	data() {
		return {
			pageLoading: false,
			exeTim: '',
			exeTimList: [],
			exportCurrentIndex: 0,
			StartDate: '01-2023',
			EndDate: '',
			ThkOpeCode: '',
			PrintType: 1,
			cachedReDatas: [],
			noDataName: [],
			format: ''
		};
	},
	components: {},
	watch: {},
	methods: {
		async onExportClick() {
			this.pageLoading = true;
			let mscNumbList = [];
			for (let i = 0; i < this.selectMacData.length; i++) {
				mscNumbList.push(this.selectMacData[i].macName);
			}
			let dataJson = {
				PltCode: this.selectMacData[0].pltCode,
				StartDate: this.StartDate || '',
				EndDate: this.EndDate || '',
				// ThkOpeCode: this.ThkOpeCode,
				ThkOpeCode: isNaN(this.exeTim) || this.exeTim == ' ' ? 0 : this.exeTim,
				PrintType: this.PrintType,
				MacType: this.selectMacData[0].macType,
				MacNumb: mscNumbList,
			}
			let rr = await ExResOfferDrawReportVerify(dataJson);
			if (rr.data.code == 0) {
				this.pageLoading = false;
				this.$alert(rr.data.data, ' ', {
					confirmButtonText: 'OK',
					type: 'warning',
				});
				return
			}
			if (!rr) {
				this.pageLoading = false;
				return;
			}
			let res = await ExResOfferDrawReport(dataJson);
			this.pageLoading = false;
			if (!res) {
				return
			}
			this.$emit("CloseExprotDiloag");
			let noDataStr = res.data.msg;
			if (noDataStr && this.PrintType == 2 && res.data.code == 2) {
				if (getLocalStorage("lang") == 0) {

					this.$alert(`${noDataStr}<p style="color:red;">${this.$t('message.public.tip5')}</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				} else {

					this.$alert(`${noDataStr}<p style="color:red;">${this.$t('message.public.tip5')}</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				}
			}
			if (res.data.data) {
				window.open(res.data.data)
			}

			// if (this.PrintType == 2) {
			// 	this.cachedReDatas = [];
			// 	this.printTypeIsTwo();
			// } else {
			// 	this.printTypeIsOne();
			// }
		},

		CloseExprotDiloag() {
			this.$emit("CloseExprotDiloag");
		},
		async printTypeIsOne() {
			this.pageLoading = true;
			let mscNumbList = [];
			for (let i = 0; i < this.selectMacData.length; i++) {
				mscNumbList.push(this.selectMacData[i].macName);
			}
			let dataJson = {
				PltCode: this.selectMacData[0].pltCode,
				StartDate: this.StartDate || '',
				EndDate: this.EndDate || '',
				// ThkOpeCode: this.ThkOpeCode,
				ThkOpeCode: isNaN(this.exeTim) || this.exeTim == ' ' ? 0 : this.exeTim,
				PrintType: this.PrintType,
				MacType: this.selectMacData[0].macType,
				MacNumb: mscNumbList,
			}
			let res = await ExResOfferDrawReportVerify(dataJson);
			console.log(res, "EEEEEEEEEEEEEEEE")
			if (res.data.code == 0) {
				// exportFlg = true;
				this.$alert(res.data.data, ' ', {
					confirmButtonText: 'OK',
					type: 'warning',
				});
				this.pageLoading = false;
				return;
			}
			let rr = await ExResOfferDrawReport(dataJson);
			console.log(rr)
			this.pageLoading = false;
			if (rr.data.code != 0) {
				this.$alert(rr.data.msg, ' ', {
					confirmButtonText: 'OK',
					type: 'warning',
				});
				this.pageLoading = false;
				return;
			}
			window.open(rr.data.data);
			this.$emit("CloseExprotDiloag");
		},
		async printTypeIsTwo() {
			this.pageLoading = true;
			let exportFlg = false;
			this.noDataName = [];
			for (let i = 0; i < this.selectMacData.length; i++) {
				let dataJson = {
					PltCode: this.selectMacData[i].pltCode,
					StartDate: this.StartDate,
					EndDate: this.EndDate,
					// ThkOpeCode: this.ThkOpeCode,
					ThkOpeCode: isNaN(this.exeTim) || this.exeTim == ' ' ? 0 : this.exeTim,
					PrintType: this.PrintType,
					MacType: this.selectMacData[i].macType,
					MacNumb: [this.selectMacData[i].macName],
				}
				let res = await ExResOfferDrawReportVerify(dataJson);
				console.log(res)
				if (res.data.code == 0) {
					exportFlg = true;
					this.$alert(res.data.data, ' ', {
						confirmButtonText: 'OK',
						type: 'warning',
					});

					this.pageLoading = false;
					break;
				}
				let rr = await ExResOfferDrawReport(dataJson);
				// if(rr.data.code != 0){
				// 	exportFlg = true;
				// 	this.$message({
				// 		type: 'error',
				// 		message: rr.data.msg
				// 	})
				// 	this.pageLoading = false;
				// 	break;
				// }
				if (rr.data.code == -310) {
					this.noDataName.push(this.selectMacData[i].macName)

					continue;
				}

				if (rr.data.code == 0) {
					this.cachedReDatas.push(rr.data.data);
				}
				console.log(rr.data.msg)
			}
			if (exportFlg) {
				this.pageLoading = false;
				return
			}
			if (this.noDataName.length <= 5) {
				var ia = 0;
				var timer = setInterval(() => {
					if (ia < this.noDataName.length) {

						this.$alert(`${this.noDataName[ia]} No Data`, '', {
							confirmButtonText: 'OK',
							type: 'warning',
							dangerouslyUseHTMLString: true,
						});
					} else {
						clearInterval(timer)
					}
					ia++;
				}, 300);
			} else {
				if (getLocalStorage("lang") == 0) {

					this.$alert(`${this.noDataName}<p style="color:red;">上記の機器は出力されませんでした。</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				} else {

					this.$alert(this.$store.state.macType == 1 ? `${this.noDataName} <p style="color:red;">There are total ${this.noDataName.length} Equipments with no date.</p>`
						: `${this.noDataName} <p style="color:red;">There are total ${this.noDataName.length} Spools  with no date.</p>`, '', {
						confirmButtonText: 'OK',
						type: 'warning',
						dangerouslyUseHTMLString: true,
					});
				}
			}
			console.log(this.cachedReDatas, "列表")
			if (this.cachedReDatas.length == 0) {
				this.pageLoading = false;
				return;
			}
			if (this.cachedReDatas.length == 1) {
				this.$emit("CloseExprotDiloag");
				window.open(this.cachedReDatas[0]);
				this.pageLoading = false;
				return;
			}
			let zipRe = await CollectFiles({
				pdfList: this.cachedReDatas,
			}).catch((err) => {
				console.log(err)
				return {
					data: {
						code: -1,
					},
				};
			});

			if (zipRe.data.code == 0) {
				this.$emit("CloseExprotDiloag");
				window.open(zipRe.data.data);
				this.pageLoading = false;
			} else {
				this.$alert(zipRe.data.msg, '', {
					confirmButtonText: 'OK',
					type: 'warning',
				});
			}
		},
		async getOfferDetialComboxApi() {
			let dataJson = {
				dmgCode: this.$store.state.dmgCode,
				macCode: this.$store.state.macCode,
				pltCode: this.$store.state.pltCode,
				rnCode: this.$store.state.prnCode || 0,
				userName: getLocalStorage("userName"),
			}
			let res = await getOfferDetialCombox(dataJson);
			console.log(res.data.data.periodDataList[0])
			this.exeTimList = res.data.data.exeTimList;
			this.exeTim = res.data.data.exeTimList[0];
			this.format = res.data.data.dateFormat;

			this.StartDate = res.data.data.periodDataList[0];
			this.EndDate = res.data.data.periodDataList[1];
			console.log(this.StartDate);
			console.log(this.EndDate);
		}

	},
	created() {
	},
	mounted() {
		console.log(this.fileRadio)
		this.getOfferDetialComboxApi();

		// let nowYear = new Date().getFullYear();
		// let nowMonth =
		// 	new Date().getMonth() + 1 < 10
		// 	? "0" + (new Date().getMonth() + 1)
		// 	: new Date().getMonth() + 1;
		// this.StartDate = `${nowYear}-${nowMonth}`;
		// this.EndDate = `${nowYear + 1}-${nowMonth}`;

	},
	beforeDestroy() { },
	destroyed() { },
};
</script>
<style lang="scss" scoped>
.export-dialog-view {
	position: fixed;
	width: 100%;
	height: 100%;
	left: 0;
	top: 0;
	z-index: 6;
}

.export-dialog {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 912px;
	height: 345px;
	// height: 292px;
	background: #ffffff;
	box-shadow: 0px 12px 48px 16px rgba(0, 0, 0, 0.05),
		0px 9px 28px 0px rgba(0, 0, 0, 0.08),
		0px 6px 16px -8px rgba(0, 0, 0, 0.15);
	display: flex;
	flex-direction: row;
	padding: 0 12px;
}

.left-lay {
	position: relative;
	width: 40%;
	height: 100%;
	// border-right: 1px solid #ccc;
	display: flex;
	flex-direction: column;
	// background-color: blanchedalmond;

	.left-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 20px;
		padding-top: 10px;

		.line-title {
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
		}

		.line-value {
			font-size: 14px;
			font-weight: 400;
			color: #7b7b7b;
			line-height: 22px;
			margin-left: 10px;
		}
	}

	.left-draw {
		position: absolute;
		top: 100vh;
		width: 750px;
		height: 611px;
		// flex-grow: 1;
		// flex-shrink: 1;
		background-color: bisque;
	}
}

.tip-lay {
	background: red;
	width: 7px;
	border-radius: 4px;
	background: #ccc;
	margin-top: 46px;
	margin: 46px 15px 85px 15px;
}

.right-lay {
	position: relative;
	width: 70%;
	height: 100%;
	display: flex;
	flex-direction: column;

	.el-icon-close {
		font-size: 16px;
		cursor: pointer;
		width: 46px;
		height: 45px;
		line-height: 45px;
	}

	.right-block-container {
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.text-right {
		padding: 0 !important;
		text-align: right;
	}

	.right-line {
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 0px 12px;
		padding-top: 17px;

		.line-title {
			font-size: 14px;
			font-weight: 700;
			color: rgba(0, 0, 0, 0.85);
			line-height: 22px;
			text-align: left;
			margin-right: 10px;
			width: 110px;
		}

		.line-content {
			flex-grow: 1;
			flex-shrink: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
		}

		.ipt {
			//     width: 368px;
			width: 100%;
			height: 32px;

			::v-deep .el-input__inner {
				width: 100%;
				height: 32px;
			}

			::v-deep .el-input__inner:focus {
				border: 1px solid #dcdfe6;
			}

			::v-deep .el-input__icon {
				line-height: 32px;
				color: #909399;
			}

			::v-deep .el-input__suffix {
				width: 32px;
				right: 0;
				background: #dee0e3;
			}
		}

		.picker {
			width: 130px;
			margin: 0px 10px;

			::v-deep .el-input__inner {
				width: 130px;
			}

			::v-deep .el-input__suffix {
				background: transparent;
			}
		}

		.orange {
			::v-deep .el-input__inner {
				// width: 130px;
				background-color: #FFDEACFF;
			}
		}

		.divider {
			width: 100%;
			height: 1px;
			background-color: #f0f0f0;
		}
	}

	.right-line.bottom-border {
		padding-bottom: 10px;
		// border-bottom: 1px solid #f0f0f0;
	}

	.flex-row {
		flex-direction: row;
	}

	.flex-column {
		flex-direction: column;
	}
}

.split-title {
	width: 100%;
	font-size: 16px;
	font-weight: 700;
	color: rgba(0, 0, 0, 0.85);
	line-height: 22px;
	padding: 12px 12px;
	border-bottom: 1px solid #f0f0f0;
}

.footer-view {
	width: calc(100% - 30px);
	box-sizing: border-box;
	position: absolute;
	z-index: 10;
	bottom: 16px;
	// padding: 0 15px;
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	// background-color: red;
	padding-top: 17px;
	border-top: 1px solid #f0f0f0;
	font-size: 16px;
	font-weight: 700;

	button {
		width: 82px !important;
		height: 32px;
		// background: #ffffff;
		// border: 1px solid #d9d9d9;
		margin-left: 12px;
		border-radius: 4px;
		font-size: 14px;
		font-weight: 500;
		line-height: 0;
	}

	button:nth-child(2) {
		// background: #106ebe;
		// color: #fff;
	}
}

.p0 {
	padding: 12px 0;
}
</style>
